package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCollectService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelCollectService travelCollectService;
    @PostMapping
    public JSONResult saveTravel(Travel travel){
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            travel.setAuthor(user);
            travelService.save(travel);
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
         return result;
    }
    //获得指定游记的id
    @GetMapping("/{id}")
    public Object getTravelById(@PathVariable Long id){
        return travelService.getTrave(id);
    }
    //更新游记操作
    @PutMapping
    public Object put(Travel travel){
        JSONResult jsonResult = new JSONResult();
        try {
            travelService.put(travel);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    //获得所有的已经发布的游记
    @GetMapping
    public Object list(TravelQueryObject qo){
        qo.setState(Travel.StATE_RELEASE);
        qo.setIsPublic(true);
        qo.setOrder("t.releaseTime desc");
        return travelService.query(qo);
    }
    //查询推荐的游记
    @GetMapping("/commends")
    public Object listCommends(TravelCommendQuery qo){

        return travelCommendService.queryCommend(qo);
    }

    //查询指定游记的所有评论,和回复的评论
    @GetMapping("/{id}/comments")
    public Object getComments(@PathVariable Long id){
        List<TravelComment> list = (List<TravelComment>) travelService.getCommentsByTravelId(id);
        List<Map<String,Object>> listMap = new ArrayList<Map<String, Object>>();

        List<Map<String,Object>> listOne = new ArrayList<Map<String, Object>>();
        if (list!=null){
        for (TravelComment comment : list) {
            //评论
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("content",comment.getContent());
            map.put("time",DateUtil.date2string(comment.getUpdatetime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("id",comment.getId());
            if (comment.getReviewer()!=null){
            map.put("userId",comment.getReviewer().getId());
            map.put("img",comment.getReviewer().getHeadImgUrl());
            map.put("replyName",comment.getReviewer().getNickName());
            }
            //回复评论
            for (CommentReply reply : comment.getReplies()) {
                HashMap<String, Object> map1 = new HashMap<String, Object>();
                if (reply.getReplyuser()!=null){
                map1.put("beReplyName",comment.getReviewer().getNickName());
                map1.put("replyName",reply.getUser().getNickName());
                }
                map1.put("content",reply.getContent());
                map1.put("time",DateUtil.date2string(reply.getCreatetime(),"yyyy-MM-dd HH:mm:ss"));

                map1.put("userId",reply.getUser().getId());
                map1.put("img",reply.getUser().getHeadImgUrl());
                listOne.add(map1);
            }
            map.put("replyBody",listOne);
            listMap.add(map);
            //每次添加之后就让listone变成新的页面
            listOne = new ArrayList<Map<String, Object>>();
        }

        }
        return listMap;
    }

    //查询收藏的游记
    @GetMapping("/{id}/collect")
    public Object selectTavelsByuserId(@PathVariable Long id,TravelQueryObject qo){

        return travelService.selectTavelsByuserId(qo,id);
    }
    @GetMapping("/get")
    //回显收藏的游记
    public  Object selectCollect(Long travelId){
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询点赞记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user == null) {
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }

            TravelCollect collect = travelCollectService.get(user.getId(), travelId);
            if (collect==null){
                jsonResult.setSuccess(false);
            }

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }



    //收藏
    @PostMapping("/collect")
    public  Object collect(Long travelId){
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询点赞记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user==null){
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
            Long userId = user.getId();
            TravelCollect travelCollect = travelCollectService.get(userId, travelId);
           if (travelCollect !=null){
               travelCollectService.delete(travelCollect.getId());
            }else {
               TravelCollect collect  = new TravelCollect();
               collect.setUser(user);
               collect.setTravel(travelService.getTrave(travelId));
               travelCollectService.save(collect);
            }

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }


        return  jsonResult;
    }




}
